/*
 * @Author: lanbin
 * @Date: 2020-11-02 14:54:43
 * @Last Modified by: lanbin
 * @Last Modified time: 2020-11-02 15:16:15
 */

import { cloneDeep } from 'lodash';

export default {
  beforeCreate() {
    const constants = this.$options.const;
    // 如果有常量
    if (constants) {
      // 保存computed
      this.$options.computed = this.$options.computed || {};

      if (Array.isArray(constants)) {
        constants.forEach((c, index) => {
          switch (typeof c) {
            case 'string':
              this.$options.computed[c.toUpperCase()] = {
                get: () => c.toUpperCase(),
                set: () => {
                  throw new Error('Cannot set a constants value');
                },
              };
              break;
            // 这个部分没有想好怎么给它取名字
            case 'object':
              this.$options.computed[`CONST_${index}`] = {
                get: () => cloneDeep(c),
                set: () => {
                  throw new Error('Cannot set a constants value');
                },
              };
              break;
          }
        });
      }
    }
  },
};
